package com.vids.task;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.vids.constant.GlobalConstant;
import com.vids.constant.Urlconstant;
import com.vids.constant.VIIDRespEnum;
import com.vids.model.entity.Register;
import com.vids.model.entity.Ape;
import com.vids.model.response.ResponseStatus;
import com.vids.mapper.ApeMapper;
import com.vids.service.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Component
public class SyncApeServerStatus implements SyncStatus {

    @Resource
    ApeMapper apeMapper;

    @Resource
    IBaseService baseService;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Override
    public ResponseStatus register(Register vo) {
        log.info("进入到设备注册1 -> {}", vo.getDeviceID());
	ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
	String apeID = "1";
	try {
        log.info("进入到设备注册2 -> {}", vo.getDeviceID());
	    if (ObjectUtil.isNull(vo)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("设备注册对象ID为空！");
            log.info("设备注册对象ID为空 -> {}", vo.getDeviceID());
		return status;
	    }
	    apeID = vo.getDeviceID();
	    // 设备注册
	    Ape ape = apeMapper.getById(apeID);
	    if (ObjectUtil.isNull(ape)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("需要添加设备对象！");
            log.info("需要添加设备对象 -> {}", vo.getDeviceID());
		return status;
	    }
	    if (ape.getIsOnline().equals(GlobalConstant.REGISTER_CODE)) {
		status.setId(apeID);
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("设备对象已经注册！");
		int i = baseService.updateAndInsertApeStatus(GlobalConstant.REGISTER_CODE, apeID);
            log.info("设备对象已经注册 -> {}", vo.getDeviceID());
		return status;
	    }
	    ape.setIsOnline("1");
	    int i = baseService.updateAndInsertApeStatus(GlobalConstant.REGISTER_CODE, apeID);
	    int j = apeMapper.updateById(ape);

	    if (j > 0) {
		String deviceStr = redisTemplate.opsForValue().get(GlobalConstant.UNREGIST_DEVICE);
		if (CharSequenceUtil.isNotBlank(deviceStr)) {
		    redisTemplate.opsForValue().set(GlobalConstant.UNREGIST_DEVICE, deviceStr.replace(apeID + ",", ""));
		}
		status.setId(apeID);
		status.setStatusString("采集设备注册成功！");
            log.info("设备注册成功 -> {}", vo.getDeviceID());
		return status;
	    }
	} catch (Exception e) {
        log.info("进入到设备注册3 -> {}", vo.getDeviceID());
	    e.printStackTrace();
	    status.setId(apeID);
	    status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
	    status.setStatusString(e.getMessage());
        log.info("设备注册报错 -> {}---{}", vo.getDeviceID(),e.getMessage());
	    return status;
	}
        log.info("进入到设备注册4 -> {}", vo.getDeviceID());
	return null;
    }

    @Override
    public void syncUpdateStatus() {
	ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
	log.info(">>>>>>>>> 更新VIIDServer 状态 >>>>>>>>>>>   间隔时间为三秒");
	// 刷离线
	baseService.updateApeStatusByDate();
	// 根据status 表 刷新状态
	apeMapper.updateIsOnlineStatuts();

    }

    @Override
    public ResponseStatus unRegister(Register vo) {
	ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
	String apeID = "1";
	try {
	    if (ObjectUtil.isNull(vo)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("设备注销对象ID为空！");
		return status;
	    }

	    apeID = vo.getDeviceID();

	    // 视图库保活
	    Ape ape = apeMapper.getById(apeID);
	    if (ObjectUtil.isNull(ape)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("需要添加设备对象！");
		return status;
	    }
	    ape.setIsOnline("2");
	    int i = baseService.updateAndInsertApeStatus(GlobalConstant.UNREGISTER_CODE, apeID);
	    int j = apeMapper.updateById(ape);
	    if (j > 0) {
		String deviceStr = redisTemplate.opsForValue().get(GlobalConstant.UNREGIST_DEVICE);
		if (CharSequenceUtil.isNotBlank(deviceStr)) {
		    redisTemplate.opsForValue().set(GlobalConstant.UNREGIST_DEVICE, deviceStr + apeID + ",");
		} else {
		    redisTemplate.opsForValue().set(GlobalConstant.UNREGIST_DEVICE, apeID + ",");
		}
		status.setId(apeID);
		status.setStatusString("设备注销成功！");
		return status;
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	    status.setId(apeID);
	    status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
	    status.setStatusString(e.getMessage());
	    return status;
	}
	return null;
    }

    @Override
    public ResponseStatus keepLive(Register vo) {
	ResponseStatus status = new ResponseStatus(Urlconstant.URL_KEEPALIVE, VIIDRespEnum.OK.getCode(), new Date());
	String apeID = "1";
	try {
	    if (ObjectUtil.isNull(vo)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("设备保活对象ID为空！");
		return status;
	    }

	    apeID = vo.getDeviceID();

	    // 视图库保活
	    Ape ape = apeMapper.getById(apeID);
	    if (ObjectUtil.isNull(ape)) {
		status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
		status.setStatusString("需要添加设备对象！");
		return status;
	    }
	    String deviceStr = redisTemplate.opsForValue().get(GlobalConstant.UNREGIST_DEVICE);
	    if (CharSequenceUtil.isNotBlank(deviceStr) && deviceStr.indexOf(apeID) != -1) {
		status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
		status.setStatusString("设备需要先注册");
		return status;
	    }
	    ape.setIsOnline("1");
	    int i = baseService.updateAndInsertApeStatus(GlobalConstant.REGISTER_CODE, apeID);
	    int j = apeMapper.updateById(ape);
	    if (j > 0) {
		status.setId(apeID);
		status.setStatusString("设备保活成功！");
		return status;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    status.setId(apeID);
	    status.setStatusCode(VIIDRespEnum.DEVICE_ERROR.getCode());
	    status.setStatusString(e.getMessage());
	    return status;
	}
	return null;
    }

    @Override
    public void time() {

    }

}
